#include "timer.h"

//定时器2中断时间为100ms，用于串口断联检测
//定时器3中断时间为10ms，用于按键消抖及检测


extern uint8_t uart6_flag;  //串口断联检测标志位
uint8_t error=0;    //错误标志

//需要按键操作的键位，与手柄按键对应
keys key[4];
extern uint8_t xbox_A;
extern uint8_t xbox_B;
extern uint8_t xbox_X;
extern uint8_t xbox_Y;
extern  float x;
extern  float y;
extern  float z;

extern "C" void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    //freeRTOS systick
    if (htim->Instance == TIM1)
    {
        HAL_IncTick();
    }
    //按键
    if(htim->Instance==TIM2)
    {
        if(uart6_flag==1)
        {
            uart6_flag=0;
            error=0;
        }
        else
        {
			#if 0
            x=0;
            y=0;
            z=0;
            error=1;
			#endif
        }

    }
    //按键长短按检测
    if(htim->Instance==TIM3)
    {
        key[0].sta = xbox_A;
        key[1].sta = xbox_B;
        key[2].sta = xbox_X;
        key[3].sta = xbox_Y;
        for(int i = 0;i < 4;i++)
        {
            switch(key[i].flag)
            {
                case 0:
                {
                    if(key[i].sta)
                    {
                        key[i].flag=1;
                    }

                }
                    break;
                case 1:
                {
                    if(key[i].sta)
                    {
                        key[i].flag = 2;
                        key[i].time = 0;
                    }

                }
                    break;
                case 2:
                {
                    if(key[i].sta == 0)
                    {
                        if(key[i].time < 100)
                        {
                            key[i].short_key = 1;
                        }
                        else
                        {
                            key[i].short_key = 1;
                            key[i].long_key = 1;
                        }
                        key[i].time = 0;
                        key[i].flag = 0;
                    }
                    else
                    {
                        key[i].time++;
                    }

                }
                    break;
            }

        }



    }
}

/*  类keys的成员函数 START    */
keys::keys():
    flag        (0),
    sta         (0),
    time        (0),
    short_key   (0),
    long_key    (0)
{

}

